/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.Admin;

import dao.LoaiPhimDAO;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.Loaiphim;

/**
 *
 * @author Dang Nhung
 */
public class Admin_FilmTypeManage extends javax.swing.JPanel {

    private DefaultTableModel model1 = null;
    List<Loaiphim> loaiPhims = null; // You must declare at here to use global
    private String arr_trash[] = {"Không hoạt động","Hoạt động"};
    
     /**
     * Declare string constant
     */
    private final String ERR_LIST_EMPTY = "Type list is empty";
    private final String ERR_SELECTED_TYPE_EMPTY = "Selected type is empty";
    private final String LABEL_SAVE = "Save";
    private final String LABEL_UPDATE = "Update Type";
    private final String LABEL_ADD = "Add Type";
    private final String LABEL_EMPTY = "";
    
    /**
     * Creates new form Admin_FilmTypeManage
     */
    public Admin_FilmTypeManage() {
        initComponents();
        initStateComponents();
        // So binding data to jtable 
        LoadDBJTable();  
    }
    
    private void clearTextFields() {
        this.jtxtcodeid.setText(LABEL_EMPTY);
        this.jtxttypename.setText(LABEL_EMPTY);
        this.jtxtdetaileddescription.setText(LABEL_EMPTY);
        jtblistoftypes.clearSelection();
        jcbtypestate.setSelectedIndex(1);
    }

     /**
     * Init state of components
     */
    private void initStateComponents() {
        // Disable all text fields
        this.jtxtcodeid.setEnabled(false);
        this.jtxttypename.setEnabled(false);
        this.jtxtdetaileddescription.setEnabled(false);
        this.jcbtypestate.setEnabled(false);

        // Disable all button except Add room button
        this.jbtnreset.setEnabled(false);
        this.jbtnaddtype.setEnabled(true);
        this.jbtnupdatetype.setEnabled(false);
        this.jbtndeletetype.setEnabled(false);
        this.jbtncancel.setEnabled(false);

        // Set text all
        this.jbtnaddtype.setText(LABEL_ADD);
        this.jbtnupdatetype.setText(LABEL_UPDATE);

        // Remove selected row
        this.jtblistoftypes.clearSelection();
    }
    
    /**
     * selected state of components while selected row in jTable
     */
    private void selectedStateComponents() {
        // Disable all text fields
        this.jtxtcodeid.setEnabled(false);
        this.jtxttypename.setEnabled(false);
        this.jtxtdetaileddescription.setEnabled(false);
        this.jcbtypestate.setEnabled(false);

        // Enable all button except Add room button
        this.jbtnreset.setEnabled(true);
        this.jbtnaddtype.setEnabled(true);
        this.jbtnupdatetype.setEnabled(true);
        this.jbtndeletetype.setEnabled(true);
        this.jbtncancel.setEnabled(true);
    }

      /**
     * update state of components while click update button
     */
    private void updateStateComponents() {
        // Enable all text fields
        this.jtxtcodeid.setEnabled(false);
        this.jtxttypename.setEnabled(true);
        this.jtxtdetaileddescription.setEnabled(true);
        this.jcbtypestate.setEnabled(true);

        // Enable all button except Add room button
        this.jbtnreset.setEnabled(true);
        this.jbtnaddtype.setEnabled(false);
        this.jbtnupdatetype.setEnabled(true);
        this.jbtndeletetype.setEnabled(false);
        this.jbtncancel.setEnabled(true);

        // Change label of update button to save
        this.jbtnupdatetype.setText(LABEL_SAVE);
    }

     /**
     * Successfully update state of components while update finished
     */
    private void updateSuccessStateComponents() {
        // Rollback init state
        initStateComponents();

        // Change label of update button to update
        this.jbtnupdatetype.setText(LABEL_UPDATE);
    }
    
    /**
     * Add state of components while add finished
     */
    private void addStateComponents() {
        // Enable all text fields
        this.jtxtcodeid.setEnabled(false);
        this.jtxttypename.setEnabled(true);
        this.jtxtdetaileddescription.setEnabled(true);
        this.jcbtypestate.setEnabled(true);

        // Enable all button except Add room button
        this.jbtnreset.setEnabled(true);
        this.jbtnaddtype.setEnabled(true);
        this.jbtnupdatetype.setEnabled(false);
        this.jbtndeletetype.setEnabled(false);
        this.jbtncancel.setEnabled(true);

        // Flush text
        clearTextFields();

        // Change label of update button to save
        this.jbtnaddtype.setText(LABEL_SAVE);
    }
    
     private void addSuccessStateComponents() {
        // Rollback init state
        initStateComponents();

        // Change label of update button to Save
        this.jbtnaddtype.setText(LABEL_SAVE);
    }
    /**
     * Author hnmtuan
     * Updated Date 16/04/2012
     * Simple binding jTable
     * Version 01
     */
    final public void LoadDBJTable() {
        model1 = (DefaultTableModel) jtblistoftypes.getModel();
        model1.getDataVector().clear();// Empty data into JTable
        try {
            loaiPhims = LoaiPhimDAO.getLoaiPhims();
            int count = 0;
            for (Loaiphim loaiphim : loaiPhims) {
                Vector v = new Vector();
                count = count + 1;
                v.addElement(count);
                v.addElement(loaiphim.getCodeLoaiPhim());
                v.addElement(loaiphim.getTenLoaiPhim());
                v.addElement(arr_trash[loaiphim.getTrangThai()]);
                v.addElement(loaiphim.getMoTa());
                model1.addRow(v);
            }
            jtblistoftypes.setModel(model1);
        } catch (Exception ex) {
            //JOptionPane.showMessageDialog(null, "Failed : " + ex.getMessage());
            ex.printStackTrace();
        }
    }
    
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jlbsearchtypeoffilm = new javax.swing.JLabel();
        jchbsearchbyname = new javax.swing.JCheckBox();
        jchbsearchbycodeid = new javax.swing.JCheckBox();
        jbtnsearch = new javax.swing.JButton();
        jbtnshowall = new javax.swing.JButton();
        jtxtsearchnameorcodeid = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jlblistoftypes = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtblistoftypes = new javax.swing.JTable();
        jpnroomanage = new javax.swing.JPanel();
        jlbcodeid = new javax.swing.JLabel();
        jlbtypename = new javax.swing.JLabel();
        jlbdetaileddescription = new javax.swing.JLabel();
        jlbtypestate = new javax.swing.JLabel();
        jtxtcodeid = new javax.swing.JTextField();
        jtxttypename = new javax.swing.JTextField();
        jtxtdetaileddescription = new javax.swing.JTextField();
        jbtnaddtype = new javax.swing.JButton();
        jcbtypestate = new javax.swing.JComboBox();
        jbtnreset = new javax.swing.JButton();
        jbtncancel = new javax.swing.JButton();
        jbtnupdatetype = new javax.swing.JButton();
        jbtndeletetype = new javax.swing.JButton();

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N

        jlbsearchtypeoffilm.setFont(new java.awt.Font("Tahoma", 3, 12)); // NOI18N
        jlbsearchtypeoffilm.setText("Search Type of Film");

        jchbsearchbyname.setText("By Name");

        jchbsearchbycodeid.setText("By Code ID");

        jbtnsearch.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jbtnsearch.setText("Search");

        jbtnshowall.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jbtnshowall.setText("Show All");

        jtxtsearchnameorcodeid.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jtxtsearchnameorcodeid.setText("Type the name or code id ...");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jlbsearchtypeoffilm, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jchbsearchbycodeid)
                    .addComponent(jchbsearchbyname))
                .addGap(18, 18, 18)
                .addComponent(jtxtsearchnameorcodeid, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jbtnsearch)
                .addGap(18, 18, 18)
                .addComponent(jbtnshowall)
                .addGap(40, 40, 40))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jtxtsearchnameorcodeid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jbtnsearch)
                            .addComponent(jbtnshowall)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jchbsearchbyname)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jchbsearchbycodeid))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jlbsearchtypeoffilm)))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jlblistoftypes.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jlblistoftypes.setText("List Of Types");

        jtblistoftypes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "STT", "Code ID", "Type's Name", "Type's State", "Detailed Description"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jtblistoftypes.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jtblistoftypes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtblistoftypesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtblistoftypes);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jlblistoftypes, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 657, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 788, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jlblistoftypes, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(270, 270, 270))
        );

        jpnroomanage.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jlbcodeid.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jlbcodeid.setText("Code ID");

        jlbtypename.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jlbtypename.setText("Type's Name");

        jlbdetaileddescription.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jlbdetaileddescription.setText("Detailed Description");

        jlbtypestate.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jlbtypestate.setText("Type's State");

        jbtnaddtype.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jbtnaddtype.setText("Add Type");
        jbtnaddtype.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnaddtypeActionPerformed(evt);
            }
        });

        jcbtypestate.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Không Hoạt động", "Hoạt động" }));

        jbtnreset.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jbtnreset.setText("Reset");
        jbtnreset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnresetActionPerformed(evt);
            }
        });

        jbtncancel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jbtncancel.setText("Cancel");
        jbtncancel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jbtncancelMouseClicked(evt);
            }
        });

        jbtnupdatetype.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jbtnupdatetype.setText("Update Type");
        jbtnupdatetype.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnupdatetypeActionPerformed(evt);
            }
        });

        jbtndeletetype.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jbtndeletetype.setText("Delete Type");
        jbtndeletetype.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtndeletetypeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpnroomanageLayout = new javax.swing.GroupLayout(jpnroomanage);
        jpnroomanage.setLayout(jpnroomanageLayout);
        jpnroomanageLayout.setHorizontalGroup(
            jpnroomanageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnroomanageLayout.createSequentialGroup()
                .addGap(114, 114, 114)
                .addGroup(jpnroomanageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpnroomanageLayout.createSequentialGroup()
                        .addGroup(jpnroomanageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jlbtypename)
                            .addComponent(jlbtypestate)
                            .addComponent(jlbcodeid)
                            .addComponent(jlbdetaileddescription))
                        .addGap(46, 46, 46)
                        .addGroup(jpnroomanageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtxtdetaileddescription, javax.swing.GroupLayout.PREFERRED_SIZE, 353, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jpnroomanageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jtxtcodeid, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jtxttypename, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jcbtypestate, javax.swing.GroupLayout.PREFERRED_SIZE, 353, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jpnroomanageLayout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(jbtnreset, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnaddtype, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnupdatetype)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtndeletetype)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtncancel, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(142, Short.MAX_VALUE))
        );
        jpnroomanageLayout.setVerticalGroup(
            jpnroomanageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnroomanageLayout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(jpnroomanageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbcodeid)
                    .addComponent(jtxtcodeid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jpnroomanageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbtypename)
                    .addComponent(jtxttypename, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jpnroomanageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbtypestate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlbtypestate))
                .addGap(18, 18, 18)
                .addGroup(jpnroomanageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtdetaileddescription, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlbdetaileddescription))
                .addGap(28, 28, 28)
                .addGroup(jpnroomanageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jbtndeletetype)
                    .addComponent(jbtnupdatetype)
                    .addComponent(jbtnreset)
                    .addComponent(jbtnaddtype)
                    .addComponent(jbtncancel))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jpnroomanage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpnroomanage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jtblistoftypesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtblistoftypesMouseClicked
        // TODO add your handling code here:
        try{
            if(loaiPhims == null){
                throw new  Exception(ERR_LIST_EMPTY);
            }
            int selectRow = jtblistoftypes.getSelectedRow(); //row which you select into JTable
            Loaiphim selectType = loaiPhims.get(selectRow);
            if (selectType == null) {
                throw new Exception(ERR_SELECTED_TYPE_EMPTY);
            }
            selectedStateComponents();
            this.jtxtcodeid.setText(selectType.getCodeLoaiPhim());
            this.jtxttypename.setText(selectType.getTenLoaiPhim());
            this.jtxtdetaileddescription.setText(selectType.getMoTa());
            this.jcbtypestate.setSelectedIndex(selectType.getTrangThai());
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }//GEN-LAST:event_jtblistoftypesMouseClicked

    private void jbtncancelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jbtncancelMouseClicked
        // TODO add your handling code here:
        this.setVisible(false);
        
        
    }//GEN-LAST:event_jbtncancelMouseClicked

    private void jbtnupdatetypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnupdatetypeActionPerformed
        // TODO add your handling code here:
        try{
            String rollAction = this.jbtnupdatetype.getText();
            // If update button is saving state.
            if(rollAction.compareToIgnoreCase(LABEL_SAVE) == 0){
                Loaiphim lp = new Loaiphim();
                int selectRow = jtblistoftypes.getSelectedRow();
                lp.setIdLoaiPhim(loaiPhims.get(selectRow).getIdLoaiPhim());
                lp.setCodeLoaiPhim(jtxtcodeid.getText());
                lp.setTenLoaiPhim(jtxttypename.getText());
                lp.setMoTa(jtxtdetaileddescription.getText());
                lp.setTrangThai(jcbtypestate.getSelectedIndex());
                if(jtxttypename.getText().equals("")|| jtxtdetaileddescription.getText().equals("")){
                    JOptionPane.showMessageDialog(null,"Name and Description aren't empty");
                    this.jbtnupdatetype.setText(LABEL_SAVE);
                    
                }
                else{
                    boolean kq = LoaiPhimDAO.updateLoaiPhims(lp);
                    LoadDBJTable();
                    updateSuccessStateComponents();
                    clearTextFields();
                }
            }else{
                updateStateComponents();
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }//GEN-LAST:event_jbtnupdatetypeActionPerformed

    private void jbtnresetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnresetActionPerformed
        // TODO add your handling code here:
        initStateComponents();
        clearTextFields();
    }//GEN-LAST:event_jbtnresetActionPerformed

    private void jbtndeletetypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtndeletetypeActionPerformed
        // TODO add your handling code here:
        try{
            int selectRow = jtblistoftypes.getSelectedRow();
            Loaiphim lp = loaiPhims.get(selectRow);
            lp.setTrangThai(0);
            boolean kq = LoaiPhimDAO.updateLoaiPhims(lp);
            if (kq == true){
                LoadDBJTable();
            }
            clearTextFields();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }//GEN-LAST:event_jbtndeletetypeActionPerformed

    private void jbtnaddtypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnaddtypeActionPerformed
        // TODO add your handling code here:
         try {
            String rollAction = this.jbtnaddtype.getText();
            // If update button is saving state
            if (rollAction.compareToIgnoreCase(LABEL_SAVE) == 0) {
                Loaiphim lp = new Loaiphim();
                List<Loaiphim> list = LoaiPhimDAO.getAllLoaiPhims();
                if (list.size()>0){
                    String temp = "Loaiphim" + ((list.get(list.size()-1).getIdLoaiPhim())+1);
                    if(jtxttypename.getText().equals("")|| jtxtdetaileddescription.getText().equals("")){
                        
                        JOptionPane.showMessageDialog(null,"Name and Description aren't empty");
                        this.jbtnaddtype.setText(LABEL_SAVE);
                    }
                    else{
                         lp.setIdLoaiPhim((list.get(list.size()-1).getIdLoaiPhim())+1);
                         lp.setCodeLoaiPhim(temp);
                         lp.setTenLoaiPhim(jtxttypename.getText());
                         lp.setMoTa(jtxtdetaileddescription.getText());
                         lp.setTrangThai(jcbtypestate.getSelectedIndex());
                         boolean kq = LoaiPhimDAO.insertLoaiPhims(lp);
                         LoadDBJTable();
                         clearTextFields();
                         addSuccessStateComponents();
                    }
                }
            } else { // if update button is updating
                addStateComponents();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_jbtnaddtypeActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnaddtype;
    private javax.swing.JButton jbtncancel;
    private javax.swing.JButton jbtndeletetype;
    private javax.swing.JButton jbtnreset;
    private javax.swing.JButton jbtnsearch;
    private javax.swing.JButton jbtnshowall;
    private javax.swing.JButton jbtnupdatetype;
    private javax.swing.JComboBox jcbtypestate;
    private javax.swing.JCheckBox jchbsearchbycodeid;
    private javax.swing.JCheckBox jchbsearchbyname;
    private javax.swing.JLabel jlbcodeid;
    private javax.swing.JLabel jlbdetaileddescription;
    private javax.swing.JLabel jlblistoftypes;
    private javax.swing.JLabel jlbsearchtypeoffilm;
    private javax.swing.JLabel jlbtypename;
    private javax.swing.JLabel jlbtypestate;
    private javax.swing.JPanel jpnroomanage;
    private javax.swing.JTable jtblistoftypes;
    private javax.swing.JTextField jtxtcodeid;
    private javax.swing.JTextField jtxtdetaileddescription;
    private javax.swing.JTextField jtxtsearchnameorcodeid;
    private javax.swing.JTextField jtxttypename;
    // End of variables declaration//GEN-END:variables
	
    private boolean saveNeeded;
}
